package com.banshuibao.bsb_android.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.InviteRecordAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.InviteDirectListModel;
import com.banshuibao.bsb_android.model.InviteInDirectListModel;
import com.banshuibao.bsb_android.model.InvitePrice;
import com.banshuibao.bsb_android.model.RequestBodyModel;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshBase;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshListView;

import java.util.List;

public class InviteRecordActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener2 {

    private PullToRefreshListView pullToRefreshListView;
    private ListView listView;
    private InviteRecordAdapter adapter;
    private TextView invite_count, direct, indirect;
    private ImageView guize;
    private LinearLayout direct_data, indirect_data;
    private Dialog dialog;
    private boolean isUp;
    private int rows, startId, endId;
    private String tag = "direct";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite_record);
        initView();
        setDirectData();
        getPrice();
    }

    public void initView() {
        setTitleText("我的邀请");
        pullToRefreshListView = findViewById(R.id.ptrlv);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(this);
        listView = pullToRefreshListView.getRefreshableView();
        listView.setDivider(null);
        listView.setVerticalScrollBarEnabled(false);
        listView.setSelector(R.color.transparent);
        listView.addHeaderView(headView());
        adapter = new InviteRecordAdapter(this);
        listView.setAdapter(adapter);
    }

    public View headView() {
        View headView = LayoutInflater.from(this).inflate(R.layout.invite_record_head_layout, null);
        invite_count = headView.findViewById(R.id.invite_count);
        guize = headView.findViewById(R.id.guize);
        direct = headView.findViewById(R.id.direct);
        indirect = headView.findViewById(R.id.indirect);
        direct_data = headView.findViewById(R.id.direct_data);
        indirect_data = headView.findViewById(R.id.indirect_data);
        TextView name = headView.findViewById(R.id.user_name);
        TextView name1 = headView.findViewById(R.id.user_name1);
        name.getPaint().setFakeBoldText(true);
        name1.getPaint().setFakeBoldText(true);
        TextView date = headView.findViewById(R.id.user_date);
        TextView price = headView.findViewById(R.id.user_price);
        date.getPaint().setFakeBoldText(true);
        price.getPaint().setFakeBoldText(true);
        TextView st = headView.findViewById(R.id.user_st);
        TextView st1 = headView.findViewById(R.id.user_st1);
        st.getPaint().setFakeBoldText(true);
        st1.getPaint().setFakeBoldText(true);
        guize.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showP();
            }
        });
        direct.setOnClickListener(listener);
        indirect.setOnClickListener(listener);
        return headView;
    }

    View.OnClickListener listener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            getPrice();
            switch (v.getId()) {
                case R.id.direct:
                    tag = "direct";
                    if (adapter != null) {
                        adapter.clear();
                    }
                    setDirectData();
                    isUp = false;
                    direct.setBackgroundResource(R.drawable.icon_invite_select);
                    indirect.setBackgroundResource(R.drawable.icon_invite_no_select);
                    indirect_data.setVisibility(View.GONE);
                    direct_data.setVisibility(View.VISIBLE);
                    break;
                case R.id.indirect:
                    tag = "indirect";
                    if (adapter != null) {
                        adapter.clear();
                    }
                    setInDirectData();
                    isUp = false;
                    indirect.setBackgroundResource(R.drawable.icon_invite_select);
                    direct.setBackgroundResource(R.drawable.icon_invite_no_select);
                    indirect_data.setVisibility(View.VISIBLE);
                    direct_data.setVisibility(View.GONE);
                    break;
            }
        }
    };

    //直接邀请
    public void setDirectData() {
        isUp = false;
        startId = 1;
        endId = startId + 9;
        if (adapter != null && adapter.getCount() > 10) {
            endId = adapter.getCount();
        }
        requestDirectNet();
    }

    //间接邀请
    public void setInDirectData() {
        isUp = false;
        startId = 1;
        endId = startId + 9;
        if (adapter != null && adapter.getCount() > 10) {
            endId = adapter.getCount();
        }
        requestInDirectNet();
    }


    public void requestDirectNet() {
        RequestBodyModel model = new RequestBodyModel();
        model.setSinceId(startId);
        model.setMaxId(endId);
        showProgressDialog();
        HttpUtil.doPost(Constants.Url.DIRECT_LIST, model, new HttpResponse(this, InviteDirectListModel.class) {
            @Override
            public void onResult(Object obj) {
                InviteDirectListModel inviteListModel = (InviteDirectListModel) obj;
                rows = inviteListModel.getRows();
                startId += rows;
                if (inviteListModel != null) {
                    List<InviteDirectListModel.DataBean> data = inviteListModel.getData();
                    if (data != null) {
                        int size = data.size();
                        if(size ==0&&adapter.getCount()==0){
                            data.add(new InviteDirectListModel.DataBean());
                        }
                    }
                    if (isUp) {
                        adapter.setDirectUpData(data);
                    } else {
                        adapter.setDirectData(data);
                    }
                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
                pullToRefreshListView.onRefreshComplete();
            }
        });
    }

    public void requestInDirectNet() {
        RequestBodyModel model = new RequestBodyModel();
        model.setSinceId(startId);
        model.setMaxId(endId);
        showProgressDialog();
        HttpUtil.doPost(Constants.Url.INDIRECT_LIST, model, new HttpResponse(this, InviteInDirectListModel.class) {
            @Override
            public void onResult(Object obj) {
                InviteInDirectListModel inviteInDirectListModel = (InviteInDirectListModel) obj;
                rows = inviteInDirectListModel.getRows();
                startId += rows;
                if (inviteInDirectListModel != null) {
                    List<InviteInDirectListModel.DataBean> data = inviteInDirectListModel.getData();
                    if (data.size()==0&&adapter.getCount()==0) {
                        data.add(new InviteInDirectListModel.DataBean());
                    }
                    if (isUp) {
                        adapter.setIndirectUpData(data);
                    } else {
                        adapter.setIndirectData(data);
                    }

                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
                pullToRefreshListView.onRefreshComplete();
            }
        });
    }

    public void getPrice() {
        HttpUtil.doPost(Constants.Url.INVITE_PRICE, null, new HttpResponse(this, InvitePrice.class) {
            @Override
            public void onResult(Object obj) {
                InvitePrice invitePrice = (InvitePrice) obj;
                if (invitePrice != null) {
                    InvitePrice.DataBean data = invitePrice.getData();
                    double sumPrice = data.getSumPrice();
                    Double aDouble = new Double(sumPrice);
                    int i = aDouble.intValue();
                    invite_count.setText(String.valueOf(i));
                    direct.setText("直接邀请人数(" + data.getOneCount() + ")");
                    indirect.setText("间接邀请人数(" + data.getTwoCount() + ")");
                }
            }
        });
    }

    public void showP() {
        View contentView = View.inflate(context, R.layout.invite_guize, null);
        if (dialog != null && dialog.isShowing()) {
            return;
        }
        if (dialog == null) {
            dialog = new AlertDialog.Builder(context).setView(contentView).create();
        }
        ImageView close = contentView.findViewById(R.id.close);
        close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog != null && dialog.isShowing()) {
                    dialog.cancel();
                    dialog.dismiss();
                }
            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        switch (tag) {
            case "direct":
                setDirectData();
                break;
            case "indirect":
                setInDirectData();
                break;
        }
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        isUp = true;
        endId = startId + 9;
        switch (tag) {
            case "direct":
                requestDirectNet();
                break;
            case "indirect":
                requestInDirectNet();
                break;
        }
    }
}
